<?php

namespace sm_shop\api\cron;
use sm_shop\controller;
use sm_shop\model\customerModel;
use sm_shop\model\goodsModel;
use sm_shop\model\cronModel;
use sm_shop\model\couponModel;
use sm_shop\model\hongbaoModel;

class cron extends controller{

    public function index(){


        $cron_list = cronModel::get_list();

        $ret = [];
        foreach( $cron_list as $key=>$cron ){

            //todo 添加 优惠券
            if( $cron['type'] == 'coupon' ){
                $data = [
                    'number'=>1,
                    'amount'=> $cron['type_value'],
                ];
                couponModel::add( $cron['customer_id'], $data );
            }
            //todo 添加 红包
            if( $cron['type'] == 'hongbao' ){

                $customer = customerModel::getCustomerById( $cron['customer_id'] );

                $hongbaoData = [

                    'customer_id'=> $cron['customer_id'],
                    'title'=> $cron['name'],
                    'amount'=> $cron['type_value'],
                    'remark'=> $cron['name'],
                ];
                hongBaoModel::record( $hongbaoData );
                $log = [
                    $customer['w7_uid'],
                    $cron['name'],
                    'sm_shop'
                ];
                mc_credit_update($customer['w7_uid'], 'credit2', $cron['type_value'], $log );

            }

            //todo 添加 计划任务执行记录
            //todo 计划任务执行次数 +1
            $recordData = [
                'customer_id'   => $cron['customer_id'],
                'cron_id'       => $cron['id']
            ];
            cronModel::record( $recordData );
            $ret[$key] = $cron;

        }
        echo json_encode( $ret );
    }

}